package com.linkage.jz.bankruptcy.activity.Person;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.event.RefreshEvent;
import com.linkage.jz.bankruptcy.fragment.MsgFragment;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.net.ToastUtil;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * A login screen that offers login via email/password.
 */
    public  class PersonSettingFeedbackActivity extends BaseActivity implements OnClickListener{
    private static final String TAG = PersonSettingFeedbackActivity.class.getSimpleName();

    private ShapeLoadingDialog shapeLoadingDialog ;
    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
        btn_save.setOnClickListener(this);
        top_back.setOnClickListener(this);
        top_mune.setOnClickListener(this);
    }

    @Override
    public void initTitle() {
        top_title = findViewById(R.id.top_title);
        top_title.setText("留言反馈");
        top_mune.setVisibility(View.VISIBLE);
        top_mune.setText("发送");
    }

    @Override
    public void initView() {

    }


    LinearLayout ll_qq,ll_email;
    TextView top_title,tv_et_content_num,top_mune,tv_mail,tv_qq;
    Button top_back,btn_save;
    EditText et_content;


    public static void start(Context context) {
        Intent intent = new Intent(context, PersonSettingFeedbackActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.activity_person_setting_feedback);

        initview();
        shapeLoadingDialog = new ShapeLoadingDialog(this);

        tv_mail = findViewById(R.id.tv_mail);
        tv_qq = findViewById(R.id.tv_qq);

        ll_qq = findViewById(R.id.ll_qq);
        ll_email = findViewById(R.id.ll_email);
        final String kefu_qq = SPUtils.getInstance(Consts.SPNAME).getString("kefu_qq");
        final String kefu_email = SPUtils.getInstance(Consts.SPNAME).getString("kefu_email");
//        ll_qq.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                EventBus.getDefault().post(new MsgFragment.ClearUnread());
//            }
//        });
//
//        ll_email.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent data=new Intent(Intent.ACTION_SENDTO);
//                data.setData(Uri.parse(kefu_email));
//                data.putExtra(Intent.EXTRA_SUBJECT, "");
//                data.putExtra(Intent.EXTRA_TEXT, "");
//                startActivity(data);
//            }
//        });


        tv_qq.setText(kefu_qq);
        tv_mail.setText(kefu_email);
        super.onCreate(savedInstanceState);
    }

    //初始化视图
    private void initview() {
        //登录层、下拉层、其它登录方式层

        btn_save = findViewById(R.id.btn_save);
        et_content = findViewById(R.id.et_content);
        top_back = findViewById(R.id.top_back);
        top_mune = findViewById(R.id.top_mune1);

        tv_et_content_num = findViewById(R.id.tv_et_content_num);

        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                        textView.setText(s.toString());

                tv_et_content_num.setText(s.length()+"/300");
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_save:
//                String content = et_content.getText().toString();
//
//                if("".equalsIgnoreCase(content))
//                {
//                    Toast.makeText(this,"内容不能为空",Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                SaveData(content);
                break;
            case R.id.top_back:
                finish();
                break;

            case R.id.top_mune1:
                String content = et_content.getText().toString();

                if("".equalsIgnoreCase(content))
                {
                    Toast.makeText(this,"内容不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }

                SaveData(content);
                break;
            default:
                break;
        }
    }

    public void SaveData(String content)
    {
        if(BankruptcyApp.tokeninfo == null|| BankruptcyApp.tokeninfo.getToken() == null)
        {
            return;
        }

        shapeLoadingDialog.setLoadingText("提交信息中...");
        shapeLoadingDialog.setCanceledOnTouchOutside(false);
        shapeLoadingDialog.show();



        Observable<JsonObject> observable = null;

            observable = Network
                    .getAPIService()
                    // TODO 参数待完善
                    .sendFeedbak(getUserToken(), content);


        observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        shapeLoadingDialog.dismiss();
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }
                        RefreshEvent event = new RefreshEvent();
                        event.setType(RefreshEvent.TYPE_TASK);
                        EventBus.getDefault().post(event);
                        showToast("提交成功");

                        finish();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        shapeLoadingDialog.dismiss();
                        super.onError(e);
                    }
                });


    }

}

